package com.forest.core.util

import java.util.regex.Pattern

object RegularUtil {

    /**
     * 邮箱验证
     *
     * @param email
     * @return
     */
    fun isEmail(email: String): Boolean {
        val pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\" + ".[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
        val matcher = pattern.matcher(email)
        return matcher.matches()
    }

    /**
     * 验证手机号
     *
     * @param phonenum  /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
     * @return
     */
    fun isPhoneNumber(phoneNumber: String): Boolean {
        val p = Pattern.compile("^((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(166)|(17[0135678])|(18[0-9])|(19[8|9]))\\d{8}$")
        val m = p.matcher(phoneNumber)
        return m.matches()
    }

    /**
     * 验证验证码
     *
     * @param phonenum
     * @return
     */
    fun isVerify(verify : String): Boolean {
        val p = Pattern.compile("^\\d{4,6}\$")
        val m = p.matcher(verify)
        return m.matches()
    }

    /**
     * 验证固定电话号
     *
     * @param phonenum
     * @return
     */
    fun isTelephone(phonenum: String): Boolean {
        val p = Pattern.compile("^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$")
        val m = p.matcher(phonenum)
        return m.matches()
    }

    /**
     * 验证密码 必须是数字与字母的混合
     *
     * @param password
     * @return
     */
    fun isPassword(password: String): Boolean {
        val p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$")
        val m = p.matcher(password)
        return m.matches()
    }

    /**
     * 验证身份证  18位数字或者最后一位为X
     *
     * @param idCard
     * @return
     */
    fun isIdCard(idCard: String): Boolean {
        val p = Pattern.compile("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$")
        val m = p.matcher(idCard)
        return m.matches()
    }


    /**
     * 汉字字母数字匹配 2-20
     *
     * @param idCard
     * @return
     */
    fun isHanZiOrNumOrLetter20(idCard: String): Boolean {
        val p = Pattern.compile("^[a-zA-Z0-9\\u4E00-\\u9FA5]{2,20}$")
        val m = p.matcher(idCard)
        return m.matches()
    }

    /**
     * 汉字字母数字匹配 2-10
     *
     * @param idCard
     * @return
     */
    fun isHanZiOrNumOrLetter10(idCard: String): Boolean {
        val p = Pattern.compile("^[a-zA-Z0-9\\u4E00-\\u9FA5]{2,10}$")
        val m = p.matcher(idCard)
        return m.matches()
    }

    /**
     * 验证银行卡考号合法性
     *
     * @param bankCard
     * @return
     */
    fun isBankCard(bankCard: String): Boolean {
        val p = Pattern.compile("^[0-9]{16,19}$")
        val m = p.matcher(bankCard)
        return m.matches()
    }

    /**
     * 验证微信号
     *
     * @param wechatId
     * @return
     */
    fun isWeiChat(wechatId: String): Boolean {
        val p = Pattern.compile("^[a-zA-Z0-9_\\-]{6,20}$")
        val m = p.matcher(wechatId)
        return m.matches()
    }

    /**
     * 验证QQ
     *
     * @param QQId
     * @return
     */
    fun isQQ(QQId: String): Boolean {
        val p = Pattern.compile("^\\d{4,13}$")
        val m = p.matcher(QQId)
        return m.matches()
    }

    /**
     * 验证用户姓名 只能为汉字和 ·2-10字
     *
     * @param userName
     * @return
     */
    fun isName(userName: String): Boolean {
        val p = Pattern.compile("^[\\u4E00-\\u9FA5\\u00b7\\u2022]{2,10}$")
        val m = p.matcher(userName)
        return m.matches()
    }

    /**
     * 验证工资 只能为正整数或小数
     *
     * @param wage
     * @return
     */
    fun isWage(wage: String): Boolean {
        val p = Pattern.compile("^(([1-9][0-9]{2,7})|([1-9][0-9]{0,}\\.[0-9]{1,2}))$")
        val m = p.matcher(wage)
        return m.matches()
    }

    fun isLegal(IDNumber: String): Boolean {//判断是否是有效的身份证号码
        var result = IDNumber.matches("[0-9]{15}|[0-9]{17}[0-9X]".toRegex())
        if (result) {
            val year: Int
            val month: Int
            val date: Int
            if (IDNumber.length == 15) {
                year = Integer.parseInt(IDNumber.substring(6, 8))
                month = Integer.parseInt(IDNumber.substring(8, 10))
                date = Integer.parseInt(IDNumber.substring(10, 12))
            } else {
                year = Integer.parseInt(IDNumber.substring(6, 10))
                month = Integer.parseInt(IDNumber.substring(10, 12))
                date = Integer.parseInt(IDNumber.substring(12, 14))
            }
            when (month) {
                2 -> result = date >= 1 && (if (year % 4 == 0) date <= 29 else date <= 28)
                1, 3, 5, 7, 8, 10, 12 -> result = date in 1..31
                4, 6, 9, 11 -> result = date in 1..31
                else -> result = false
            }
        }
        return result
    }

    /**
     * Url链接验证
     *
     * @param url
     * @return
     */
    fun isHttpUrl(url: String): Boolean {
        val pattern = Pattern.compile("(http|ftp|https)://[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-.,@?^=%&amp;:/~+#]*[\\w\\-@?^=%&amp;/~+#])?")
        val matcher = pattern.matcher(url)
        return matcher.matches()
    }

    fun isComUrl(url: String): Boolean {
        val pattern = Pattern.compile("^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9\\-.]+\\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)*(/($|[a-zA-Z0-9.,;?'\\\\+&amp;%$#=~_\\-]+))*$")
        val matcher = pattern.matcher(url)
        return matcher.matches()
    }
}
